layui.config({
    base: "/static/backend/js/"
}).use(['form', 'layer', 'jquery', 'table'], function () {
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : parent.layer,
        table = layui.table,
        $ = layui.jquery;

    //第一个实例
    table.render({
        elem: '#list'
        , height: '700'
        ,toolbar: '#toolbar'

        , url: '/backend/goods/goodsList' //数据接口
        , page: true //开启分页
        , cols: [[ //表头
            {type:'checkbox',}
            ,{field: 'id', title: 'ID', width:'10%', sort: false}
            , {field: 'cat_name',title: '商品分类',}
            , {field: 'name',title: '商品名称',}
            , {field: 'poster',title: '商品海报',templet:'<div><img style="overflow: hidden;height:48px;width:48px;" src="{{ d.poster}}"></div>'}
            , {field: 'amount', align:'center',title: '库存', sort: false}
            , {field: 'pageviews', align:'center',title: '浏览量', sort: false}
            , {field: 'sort', align:'center',title: '排序值', sort: false}
            , {field: 'is_on', align:'center',title: '是否上架', sort: false}
            /*            , {field: 'create_time', width:'20%',title: '注册时间', sort: true}*/
            , {field: 'right', align:'center',title: '操作', toolbar: '#barDemo'}

        ]]
    });


    $(".add").click(function () {
        layui.layer.open({
            title : "添加商品",
            type : 2,
            area: ['85%', '85%'],
            offset: ['5%', '12%'],
            content : "/backend/goods/addGoods",
            success : function(layero, index){
            }
        })
    })



    table.on('tool(goods)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
        var data = obj.data; //获得当前行数据
        var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
        var tr = obj.tr; //获得当前行 tr 的DOM对象
        var id=data.id;
        if(layEvent === 'detail'){ 
            
            layui.layer.open({
                title : "编辑商品",
                type : 2,
                area: ['85%', '85%'],
                offset: ['5%', '12%'],
                content : "/backend/goods/detail/id/"+id,
                success : function(layero, index){
                }
            })
        } else if(layEvent === 'down'){ //下架
            layer.confirm('确定执行下架操作？', function(index){
                layer.close(index);
                $.post('/backend/goods/caozuo_goods', {
                        id: id
                    }, function (data) {
                        if (data.status ==1) {
                            top.layer.msg(data.msg);
                            location.reload();
                        } else {
                            top.layer.msg(data.msg);
                        }

                    }
                );
                //向服务端发送删除指令
            });
        } else if(layEvent === 'up'){//上架
            layer.confirm('确定执行上架操作？', function(index){
                layer.close(index);
                $.post('/backend/goods/caozuo_goods', {
                        id: id
                    }, function (data) {
                        if (data.status ==1) {
                            top.layer.msg(data.msg);
                            location.reload();
                        } else {
                            top.layer.msg(data.msg);
                        }

                    }
                );
                //向服务端发送删除指令
            });
        }
    });


    //批量删除
    $(".batchDel").click(function(){
        var checkStatus = table.checkStatus('list');
        // console.log(checkStatus);
        if(checkStatus.data.length>0){
            var datalist={};
            for(var i=0;i<checkStatus.data.length;i++){
                datalist[i]=checkStatus.data[i].id;
            }
            // console.log(datalist);
            layer.confirm('真的删除'+checkStatus.data.length+'条？', function(index){
                layer.close(index);
                $.post('/backend/goods/deleteGoods', {
                        id: datalist
                    }, function (data) {
                        if (data) {
                            top.layer.msg("删除成功");
                            window.location.reload();
                        } else {
                            top.layer.msg("出错了！！！");
                        }

                    }
                );
            });
        }else{
            top.layer.msg("请选择数据！！");
        }
    })

    $('.search_btn').click(function () {

        var goods_name= $('.searchVal').val();

        console.log(goods_name);

        table.reload('list', {

            url: '/backend/goods/goodsList'

            ,where: {goods_name:goods_name} //设定异步数据接口的额外参数

        });

    })









})